import GameConfig from "GameConfig";
import GameUiTools from "GameUiTools";

cc.Class({
    extends: cc.Component,

    properties: {
        countThree: cc.Node,
        countTwo: cc.Node,
        countOne: cc.Node,
        countDownAudio: {
            type: cc.AudioClip,
            default: null,
        },
    },

    init(player) {
        this.player = player;
    },

    onLoad () {
        this.countArray = new Array();
        this.countArray[0] = this.countThree;
        this.countArray[1] = this.countTwo;
        this.countArray[2] = this.countOne;
    },

    start () {
        var countDownAudioId = null;
        // 倒计时
        var component = this.getComponent("CountDown");
        this.count = 0;
        this.callback = function () {
            if (null == countDownAudioId) {
                countDownAudioId = cc.audioEngine.playEffect(this.countDownAudio, false);
            }
            if (this.count === 3) {
                if (countDownAudioId) {
                    cc.audioEngine.stop(countDownAudioId);
                }
                // 在第3次执行回调时取消这个计时器
                GameConfig.GAME_READY = true;
                this.player.goRun();
                GameUiTools.unLoadingLayer(this.node);
                this.unschedule(this.callback);
            }
            for(var i = 0; i < 3; i++) {
                if (this.count === i) {
                    this.countArray[i].opacity = 255;
                } else {
                    this.countArray[i].opacity = 0;
                }
            }
            if (this.count < 3) {
                let spawn = cc.spawn(cc.scaleTo(1, 1.3, 1.3), cc.fadeOut(1) );
                this.countArray[this.count].runAction(spawn);
            }
            this.count++;
        }
        component.schedule(this.callback, 1);
    },
});
